package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.BrabchBean;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.Money2Bean;
import com.zfjt.entity.MoneyBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00027;
import com.zfjt.service.BrabchService;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.service.UserService;
import com.zfjt.utils.StringUtil;

/**
 * 查询业绩
 * @author xk
 * @version 1.0
 * @date 2017年4月10日
 */
@Controller
public class PerformanceController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	@Autowired
	private BrabchService mBrabch;
	
	@Autowired
	private UserService mUser;
	
	private Map<String, String> fhdata; 
	
	private List<ClientmessageBean> mAllData;
	
	private List<BrabchBean> mBrabchData;
	
	private List<MoneyBean> mFh;
	
	private List<Money2Bean> mFh2;
	
	@RequestMapping(value="00027")
	@ResponseBody
	public ReturnBean RemoveOrder(HttpSession hseesion){
		mAllData = new ArrayList<>();
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		mFh = new ArrayList<>();
		mFh2 = new ArrayList<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00027 num00027 = JSON.parseObject(data,PackNum00027.class); 	
		List<ClientmessageBean> mList = mClientMessage.selectAll();
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00027");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			if (num00027.getType().equals("0")) {//当管理权限为0时，算出所有网点的业绩
				List<BrabchBean> sywd = mBrabch.selectAll();
				for (int i = 0; i < sywd.size(); i++) {
					BrabchBean dg = sywd.get(i);
					int num = 0;
					String id = sywd.get(i).getBrabchId();
					num = num + Integer.parseInt(dg.getAffiliation());
					int s = Integer.parseInt(dg.getAffiliation());
					mFh2.add(new Money2Bean(s, sywd.get(i).getBrabchName(), sywd.get(i).getBrabchId()));
				}
				mReturn.setCode("000");
				mReturn.setPortnum("00027");
				mReturn.setData(JSON.toJSONString(mFh2));
			}else if (num00027.getType().equals("2")) {//当管理权限为2时，算出该网点的业绩总和
				//算出该网点人数，存入mBrUser
				List<UserBean> mUserData = new ArrayList<>();
				List<UserBean> mBrUser = new ArrayList<>();
				mUserData = mUser.selectAll();
				for (int i = 0; i < mUserData.size(); i++) {
					UserBean dg = mUserData.get(i);
					if (dg.getBranch().equals(num00027.getBrabch_id())) {
						mBrUser.add(dg);
					}
				}
				//算出每个人的业绩总和
				for (int i = 0; i < mBrUser.size(); i++) {
					UserBean dg = mBrUser.get(i);
					int num = Integer.parseInt(dg.getText());
					mFh.add(new MoneyBean(num, dg.getName()));
				}
				mReturn.setCode("000");
				mReturn.setPortnum("00027");
				mReturn.setData(JSON.toJSONString(mFh));
			}
		}
		return mReturn;
	}
}
